package com.sz.web.common.control;

import com.sz.web.account.enums.CodeName;
import com.sz.web.util.CodeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping(value = "/code")
public class UtilControl {

    @RequestMapping(value = "/getCode")
    public void getCode(HttpServletRequest reqeust, HttpServletResponse response){

        response.setContentType("image/jpeg");
        // 禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        HttpSession session = reqeust.getSession();

        CodeUtil vCode = new CodeUtil(100, 28, 4, 50);
        session.setAttribute(CodeName.Code.getName(), vCode.getCode());
        try {
            vCode.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
